package com.hrp.book.utils

import com.blankj.utilcode.util.FileIOUtils
import com.google.gson.Gson
import java.io.BufferedInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream

object ConverterUtils {


    inline fun <reified T> Gson.fromJson(json: String): T {
        return fromJson(json, T::class.java)
    }

    fun getString(inputStream: InputStream): String {
        return inputStream.use useInput@{ stream ->

            val input: InputStream =
                BufferedInputStream(stream, FileIOUtils.sBufferSize)
            val output = ByteArrayOutputStream()
            return@useInput output.use useOutput@{
                val b = ByteArray(FileIOUtils.sBufferSize)
                var len: Int
                while (input.read(b, 0, FileIOUtils.sBufferSize).also { len = it } != -1) {
                    output.write(b, 0, len)
                }
                output.flush()
                return@useOutput String(output.toByteArray(), Charsets.UTF_8)
            }
        }
    }
}